package com.xb.cloud.merchantapi.product.service.impl;

import org.springframework.stereotype.Component;

import com.xb.cloud.common.util.StringUtils;
import com.xb.cloud.common.util.VaildateUtils;
import com.xb.cloud.gateway.client.ReturnCodes;
import com.xb.cloud.gateway.client.ReturnUtils;
import com.xb.cloud.gateway.client.RtnInfo;
@Component
public class MerchantPassportServiceValidator {
	public RtnInfo validPhoneWithPassword(String phoneNum, String password) {
		RtnInfo rtnInfo = validatePhoneNum(phoneNum);
		if (ReturnUtils.isError(rtnInfo)) {
			return validatePassword(password);
		}
		return rtnInfo;
	}

	public RtnInfo validPhoneWithVerifyCode(String phoneNum, String verifyCode) {
		RtnInfo rtnInfo = validatePhoneNum(phoneNum);
		if (ReturnUtils.isError(rtnInfo)) {
			return validateVerifyCode(verifyCode);
		}
		return rtnInfo;
	}

	public RtnInfo validatePhoneNum(String phoneNum) {
		if (StringUtils.isEmpty(phoneNum)) {
			return ReturnUtils.error(ReturnCodes.PHONENUM_REQUIRED);
		}
		if (!VaildateUtils.isMobile(phoneNum)) {
			return ReturnUtils.error(ReturnCodes.PHONENUM_INVALID);
		}
		return ReturnUtils.success();
	}

	public RtnInfo validatePassword(String password) {
		if (StringUtils.isEmpty(password)) {
			return ReturnUtils.error(ReturnCodes.PASSWORD_REQUIRED);
		}
		if (!VaildateUtils.isPassword(password)) {
			return ReturnUtils.error(ReturnCodes.PASSWORD_INVALID);
		}
		return ReturnUtils.success();
	}

	public RtnInfo validateVerifyCode(String verifyCode) {
		if (StringUtils.isEmpty(verifyCode)) {
			return ReturnUtils.error(ReturnCodes.VERIFYCODE_REQUIRED);
		}
		if (!VaildateUtils.isVerifyCode(verifyCode)) {
			return ReturnUtils.error(ReturnCodes.VERIFYCODE_INVALID);
		}
		return ReturnUtils.success();
	}
}
